package com.example.backend.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.example.backend.common.EncryptUtil;
import com.example.backend.entity.DesignProduct;
import com.example.backend.entity.User;
import com.example.backend.entity.UserInfo;
import com.example.backend.mapper.UserInfoMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static org.springframework.web.bind.annotation.RequestMethod.*;

@RestController
public class UserInfoController {
    @Resource
    UserInfoMapper userInfoMapper;
    @RequestMapping(value ="/getOrders",method = GET)
    String getOrder(@RequestParam(value = "openId", required = true) String openId){
        QueryWrapper<UserInfo> wrapper=new QueryWrapper<>();
        wrapper.eq("open_id",openId).eq("type",1);
        List<UserInfo>userInfos=userInfoMapper.selectList(wrapper);
        userInfos=userInfos.stream().sorted(Comparator.comparing(UserInfo::getDate).reversed()).collect(Collectors.toList());
        System.out.println(userInfos);
        String result=null;
        ObjectMapper mapper = new ObjectMapper();
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
        try {
            result= EncryptUtil.encrypt(Convert.toStr(mapper.writeValueAsString(userInfos)));
        }catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping(value ="/getCouples",method = GET)
    String getCouples(@RequestParam(value = "openId", required = true) String openId){
        QueryWrapper<UserInfo> wrapper=new QueryWrapper<>();
        wrapper.eq("open_id",openId).eq("type",2);
        List<UserInfo>userInfos=userInfoMapper.selectList(wrapper);
        userInfos=userInfos.stream().sorted(Comparator.comparing(UserInfo::getDate).reversed()).collect(Collectors.toList());
        System.out.println(userInfos);
        String result=null;
        ObjectMapper mapper = new ObjectMapper();
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
        try {
            result= EncryptUtil.encrypt(Convert.toStr(mapper.writeValueAsString(userInfos)));
        }catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    @RequestMapping(value = "/makeOrder",method = POST)
    void makeAnOrder(@RequestParam(value="message",required=true)String message){
        String result=null;
        System.out.println(message);
        try {
            result= EncryptUtil.decrypt(message);
        }catch (Exception e) {
            e.printStackTrace();
        }
        String str[]=result.split("@");
        Date date= DateUtil.parse(str[str.length-1]);
        String openId=str[str.length-2];
        System.out.println(date);
        System.out.println(openId);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //new一个Calendar类 把Date放入进去
        Calendar instance = Calendar.getInstance();
        instance.setTime(date);
        instance.add(Calendar.HOUR, -8);
        //转换成String类型
        String format = simpleDateFormat.format(instance.getTime());
        DateTime newDate=DateUtil.parse(format);
        System.out.println(newDate);
        UserInfo userInfo=new UserInfo();
        userInfo.setOpenId(openId);
        userInfo.setMessage(result);
        userInfo.setDate(newDate);
        userInfo.setType("1");
        System.out.println(userInfo.toString());
        userInfoMapper.insert(userInfo);
    }

    @RequestMapping(value = "/makeCouple",method = POST)
    void makeACouple(@RequestParam(value="message",required=true)String message){
        String result=null;
        try {
            result= EncryptUtil.decrypt(message);
        }catch (Exception e) {
            e.printStackTrace();
        }
        String str[]=result.split("@");
        Date date= DateUtil.parse(str[str.length-2]);
        String openId=str[str.length-3];
        System.out.println(date);
        System.out.println(openId);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //new一个Calendar类 把Date放入进去
        Calendar instance = Calendar.getInstance();
        instance.setTime(date);
        instance.add(Calendar.DAY_OF_MONTH, +7);
        instance.add(Calendar.HOUR, -8);
        //转换成String类型
        String format = simpleDateFormat.format(instance.getTime());
        DateTime newDate=DateUtil.parse(format);
        UserInfo userInfo=new UserInfo();
        System.out.println(newDate);
        userInfo.setOpenId(openId);
        userInfo.setMessage(result);
        userInfo.setDate(newDate);
        userInfo.setType("2");
        userInfoMapper.insert(userInfo);
    }
    @RequestMapping(value = "/useCouple",method = PUT)
    void useCouple(@RequestParam(value="id",required=true)int id){
        UserInfo userInfo=userInfoMapper.selectById(id);
        String str=userInfo.getMessage();
        str=str.replace("unused","used");
        LambdaUpdateWrapper<UserInfo> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(UserInfo::getId,id).set(UserInfo::getMessage,str);
        userInfoMapper.update(null,lambdaUpdateWrapper);
    }
}
